package net.example.lesson1;

//import lombok.extern.slf4j.Slf4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.util.Map;

/**
 * @author zhuhai
 *
 */
//@Slf4j
public class ExceptionDemo {
    private final static Logger log = LoggerFactory.getLogger(ExceptionDemo.class);
    /**
     * 打印
     * Hello World
     * over
     */
    public void tryCatchFinally() {
        try {
            log.info("Hello World");
        } catch (Exception e) {
            log.info("Exception:"+e.getMessage());
        } finally {
            log.info("over");
        }
    }

    /**
     * 1、不忽略异常
     * 2、不捕获通用异常
     */
    public void test1() {
        try {
            throwException();
        } catch(Exception e) {
        }
    }
    /**
     * finally  里代码不会执行
     */
    public void test2() {
        try {
            System.exit(1);
        } finally {
            log.info("over");
        }
    }
    /**
     * finally  里代码不会执行
     */
    public void test3() {
        try {
            while (true){

            }
        } finally {
            System.out.println("over");
        }
    }

    /**
     * 正确捕获异常
     */
    public void originMultiCatch() {
        try {
            throwException();
        } catch(ConnectException e) {
            //不用此方式打印异常
            e.printStackTrace();
        } catch (NullPointerException e) {
            //使用此方式打印异常
            log.error("###异常###:{}", e);
        } catch (CustException e) {
            log.info(e.getName());
        }
    }

    /**
     * 正确捕获异常
     */
    public void multiCatch() {
        try {
            throwException();
        } catch (NullPointerException | ConnectException | CustException e) {
            log.error("###异常###:{}", e.getMessage());
        }
    }
    public void throwException() throws ConnectException, NullPointerException, CustException {
        final Map map = null;
        log.info("信息{}", map.get("name"));
    }

    /**
     * JDK1.7之前关闭资源方式.
     */
    public void originClose() {
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File("D:\\11.txt"));
            System.out.println(inputStream.read());
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * JDK1.7以后使用如下方式
     */
    public void tryWithResource() {
        try (FileInputStream inputStream = new FileInputStream(new File("D:\\11.txt"))) {
            System.out.println(inputStream.read());
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public static void main(String[] args) {
        final ExceptionDemo ed = new ExceptionDemo();
//        ed.tryCatchFinally();
//        ed.originMultiCatch();
//        ed.multiCatch();
//        ed.originClose();
//        ed.tryWithResource();
    }
}
